import 'package:flutter/material.dart';
import 'dart:math';

class DragScaleWidget extends StatefulWidget {
  final Widget child;
  final bool enableRotation;
  final bool enableScaling;
  final bool enableDragging;

  const DragScaleWidget({
    super.key,
    required this.child,
    this.enableRotation = true,
    this.enableScaling = true,
    this.enableDragging = true,
  });

  @override
  State<DragScaleWidget> createState() => _DragScaleWidgetState();
}

class _DragScaleWidgetState extends State<DragScaleWidget> {
  double _scale = 1.0;
  double _previousScale = 1.0;
  Offset _position = Offset.zero;
  Offset _previousPosition = Offset.zero;
  double _rotation = 0.0;
  double _previousRotation = 0.0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onScaleStart: (details) {
        _previousScale = _scale;
        _previousPosition = details.focalPoint;
        _previousRotation = _rotation;
      },
      onScaleUpdate: (details) {
        setState(() {
          if (widget.enableScaling) {
            _scale = _previousScale * details.scale;
          }
          if (widget.enableRotation) {
            _rotation = _previousRotation + details.rotation;
          }
          if (widget.enableDragging) {
            _position += details.focalPoint - _previousPosition;
            _previousPosition = details.focalPoint;
          }
        });
      },
      child: Transform(
        transform: Matrix4.identity()
          ..translate(_position.dx, _position.dy)
          ..rotateZ(_rotation)
          ..scale(_scale),
        alignment: Alignment.center,
        child: widget.child,
      ),
    );
  }
}


/*

DragScaleWidget(
  enableDragging: true,  // 允许拖动
  enableScaling: true,   // 允许缩放
  enableRotation: true,  // 允许旋转
  child: Image.network('https://example.com/image.jpg'),
)

*/